define(function (require, exports, module) {
        exports.init = function (element, cb) {
                var basic=require('basic');
                var jsonAPI = require('json-api');
                var mustache = require('mustache');
                var async=require('async');
                var checkVehicles=[];



                $("#title").text('车辆列表');
                defineModule({
                        listVehicle:function(cb)
                        {
                                var mobile=basic.getLoginUser();
                                jsonAPI.restful('/api/vehicle/list', {mobile:mobile}, function (err, result) {
                                        if (jsonAPI.handleErrors(err, result)) {
                                                var tpl = $('#vehicleList').html();
                                                var content = mustache.render(tpl, mustache.normalizeInput(result));
                                                $('#vehicles').html(content);
                                        }
                                        cb && cb();
                                });
                        },
                        triggerStatus:function()
                        {
                                checkVehicles.clear();
                                 $("#vehicles").find('input').each(function(i,checkbox)
                                 {
                                         var checked=$(this).attr("checked");
                                         if(checked)
                                         {
                                                 var licenseplate=$(this).parent().parent().find('p').html();
                                                 checkVehicles.push(licenseplate);
                                         }
                                 })
                                if(checkVehicles.length>0)
                                {
                                        $("#delVehicle").removeClass ('weui_btn_disabled');
                                }
                                else
                                        $("#delVehicle").addClass('weui_btn_disabled');

                        },
                        deleteVehicle:function()
                        {
                                if(checkVehicles.length<=0)
                                {
                                        return;
                                }
                                else
                                {
                                        function _doDelete()
                                        {
                                                var mobile=basic.getLoginUser();
                                                var calls=[];
                                                for(var i=0;i<checkVehicles.length;i++)
                                                {
                                                        (function(licenseplate){
                                                                var fn = function(cb) {
                                                                        var param={mobile:mobile,licenseplate:licenseplate}
                                                                        jsonAPI.restful('/api/vehicle/delete',param, function (err, result) {
                                                                                cb(err, result);
                                                                        });
                                                                };
                                                                calls.push(fn);
                                                        })(checkVehicles[i]);
                                                }

                                                async.parallel(calls,function(err,result)
                                                {
                                                        if (jsonAPI.handleErrors(err, result)) {
                                                                $spa.showOk('删除成功');
                                                                setTimeout(function () {
                                                                        $spa.hideOk();
                                                                        $module.listVehicle();
                                                                }, 1000);

                                                        }
                                                })
                                        }

                                        $spa.confirm('删除车辆','确定要删除选中的车辆么?',function()
                                        {
                                                _doDelete();
                                        });
                                }
                        },
                        addVehicle:function()
                        {
                                window.hashTo("#addVehicle");
                        }
                })

                $module.listVehicle(function()
                {
                        cb && cb();
                });

        };
});